package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.SearchContactAdapter;
import com.bnyy.message.bean.Contact;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.ContactUser;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/12
 **/
public class SearchContactActivity extends BaseActivityImpl {

    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.et_search)
    EditText etSearch;

    private HashMap<String, ArrayList<? extends Contact>> datas;
    private SearchContactAdapter adapter;

    public static void show(Context context, HashMap<String, ArrayList<? extends Contact>> datas, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, SearchContactActivity.class);
        intent.putExtra("datas", datas);
        launcher.launch(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_serach_contact;
    }

    @Override
    public String getTitleStr() {
        return "搜索";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        datas = (HashMap<String, ArrayList<? extends Contact>>) getIntent().getSerializableExtra("datas");

        if (datas == null || datas.size() == 0) {
            finish();
        } else {
            adapter = new SearchContactAdapter(mContext, new Consumer<Contact>() {
                @Override
                public void accept(Contact contact) {
                    Intent intent = new Intent();
                    intent.putExtra("contact", contact);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });

            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(adapter);
            adapter.refresh(datas);

            etSearch.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                        Editable text = etSearch.getText();
                        if (!TextUtils.isEmpty(text)) {
                            String keyword = text.toString();

                            HashMap<String, ArrayList<? extends Contact>> targetMap = new HashMap<>();
                            datas.forEach(new BiConsumer<String, ArrayList<? extends Contact>>() {
                                @Override
                                public void accept(String s, ArrayList<? extends Contact> contacts) {
                                    ArrayList<Contact> targetContacts = new ArrayList<>();
                                    contacts.forEach(new Consumer<Contact>() {
                                        @Override
                                        public void accept(Contact contact) {
                                            String name = "";
                                            if (contact instanceof ContactUser) {
                                                ContactUser contactUser = (ContactUser) contact;
                                                name = contactUser.getUsername();
                                            } else if (contact instanceof ContactGroup) {
                                                ContactGroup contactGroup = (ContactGroup) contact;
                                                name = contactGroup.getGroup_name();
                                            }

                                            if (name.contains(keyword)) {
                                                targetContacts.add(contact);
                                            }
                                        }
                                    });

                                    if (targetContacts.size() > 0) {
                                        targetMap.put(s, targetContacts);
                                    }
                                }
                            });
                            if (targetMap.size() > 0) {
                                adapter.refresh(targetMap);
                            } else {
                                Toast.makeText(mContext, "无匹配的联系人", Toast.LENGTH_SHORT).show();
                            }
                            return true;
                        }
                    }
                    return false;
                }
            });
            etSearch.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    ivClear.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
                }
            });

            ivClear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    etSearch.setText("");
                    adapter.refresh(datas);
                }
            });
        }
    }
}











































